package com.ruoyi.activiti.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ruoyi.activiti.domain.ActMaintainOrderData;
import com.ruoyi.activiti.domain.vo.ActMaintainOrderDataVO;
import com.ruoyi.activiti.mapper.ActMaintainOrderDataMapper;
import com.ruoyi.activiti.service.IActMaintainOrderDataService;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 单数据Service业务层处理
 *
 * @author cjie
 * @date 2022-04-04
 */
@Service
public class ActMaintainOrderDataServiceImpl implements IActMaintainOrderDataService {
    @Autowired
    private ActMaintainOrderDataMapper actMaintainOrderDataMapper;

    /**
     * 查询单数据
     *
     * @param id 单数据ID
     * @return 单数据
     */
    @Override
    public ActMaintainOrderData selectActMaintainOrderDataById(String id) {
        return actMaintainOrderDataMapper.selectActMaintainOrderDataById(id);
    }

    /**
     * 查询单数据列表
     *
     * @param actMaintainOrderData 单数据
     * @return 单数据
     */
    @Override
    public List<ActMaintainOrderData> selectActMaintainOrderDataList(ActMaintainOrderData actMaintainOrderData) {
        return actMaintainOrderDataMapper.selectActMaintainOrderDataList(actMaintainOrderData);
    }

    /**
     * 新增单数据
     *
     * @param actMaintainOrderData 单数据
     * @return 结果
     */
    @Override
    public int insertActMaintainOrderData(ActMaintainOrderData actMaintainOrderData) {
        actMaintainOrderData.setId(IdUtil.getSnowflake(0, 0).nextIdStr());
        actMaintainOrderData.setCreateTime(DateUtils.getNowDate());
        actMaintainOrderData.setCreateBy(SecurityUtils.getUsername());
        return actMaintainOrderDataMapper.insertActMaintainOrderData(actMaintainOrderData);
    }

    /**
     * 修改单数据
     *
     * @param actMaintainOrderData 单数据
     * @return 结果
     */
    @Override
    public int updateActMaintainOrderData(ActMaintainOrderData actMaintainOrderData) {
        actMaintainOrderData.setUpdateTime(DateUtils.getNowDate());
        actMaintainOrderData.setUpdateBy(SecurityUtils.getUsername());
        return actMaintainOrderDataMapper.updateActMaintainOrderData(actMaintainOrderData);
    }

    /**
     * 批量删除单数据
     *
     * @param ids 需要删除的单数据ID
     * @return 结果
     */
    @Override
    public int deleteActMaintainOrderDataByIds(String[] ids) {
        return actMaintainOrderDataMapper.deleteActMaintainOrderDataByIds(ids);
    }

    /**
     * 删除单数据信息
     *
     * @param id 单数据ID
     * @return 结果
     */
    @Override
    public int deleteActMaintainOrderDataById(String id) {
        return actMaintainOrderDataMapper.deleteActMaintainOrderDataById(id);
    }

    @Override
    public int insertActMaintainOrderDataVO(ActMaintainOrderDataVO actMaintainOrderDataVO) {
        //this.checkFields(actMaintainOrderVO.getFields());
        // 插入
        ActMaintainOrderData actMaintainOrderData = new ActMaintainOrderData();
        BeanUtils.copyProperties(actMaintainOrderDataVO,actMaintainOrderData);
        //ActMaintainOrder actMaintainOrder = ActMaintainOrderConvert.INSTANCE.convert(actMaintainOrderVO);

        actMaintainOrderData.setFormVariables(covertMap2One(actMaintainOrderDataVO.getFormVariables()));
//        BpmFormDO form = BpmFormConvert.INSTANCE.convert(createReqVO);
//        formMapper.insert(form);
//        // 返回
//        return form.getId();
        if(actMaintainOrderDataVO.getId() == null){
            return this.insertActMaintainOrderData(actMaintainOrderData);
        }else {
            return this.updateActMaintainOrderData(actMaintainOrderData);
        }

    }

    @Override
    public int updateActMaintainOrderDataVO(ActMaintainOrderDataVO actMaintainOrderDataVO) {
        //this.checkFields(actMaintainOrderVO.getFields());
        // 插入
        ActMaintainOrderData actMaintainOrderData = new ActMaintainOrderData();
        BeanUtils.copyProperties(actMaintainOrderDataVO,actMaintainOrderData);
        //ActMaintainOrder actMaintainOrder = ActMaintainOrderConvert.INSTANCE.convert(actMaintainOrderVO);

        actMaintainOrderData.setFormVariables(covertMap2One(actMaintainOrderDataVO.getFormVariables()));
//        BpmFormDO form = BpmFormConvert.INSTANCE.convert(createReqVO);
//        formMapper.insert(form);
//        // 返回
//        return form.getId();
        return this.updateActMaintainOrderData(actMaintainOrderData);
    }

    @Override
    public ActMaintainOrderDataVO selectActMaintainOrderDataVOById(String id) {
        ActMaintainOrderDataVO actMaintainOrderDataVO = new ActMaintainOrderDataVO();
        ActMaintainOrderData actMaintainOrderData = this.selectActMaintainOrderDataById(id);
        BeanUtils.copyProperties(actMaintainOrderData,actMaintainOrderDataVO);
        actMaintainOrderDataVO.setFormFields(this.convertOne2List(actMaintainOrderData.getFormFields()));
        actMaintainOrderDataVO.setFormVariables(this.convertOne2Map(actMaintainOrderData.getFormVariables()));
        return actMaintainOrderDataVO;
    }

    private String covertMap2One(Map<String,Object> arr){
        return JSONUtil.toJsonStr(arr);
    }

    private Map<String,Object> convertOne2Map(String one){
        //return Arrays.asList(one.split(";")).stream().map(s->(s.trim())).collect(Collectors.toList());
        Map<String, Object> map = new HashMap<String, Object>();
        JSONObject jsonObject = JSONUtil.parseObj(one);
        Iterator iterator = jsonObject.entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry<String, Object> entry = (Map.Entry<String, Object>)iterator.next();
            map.put(entry.getKey(),entry.getValue());
        }
        return map;
    }

    private List<String> convertOne2List(String one){
        //return Arrays.asList(one.split(";")).stream().map(s->(s.trim())).collect(Collectors.toList());
        return JSONUtil.toList(JSONUtil.parseArray(one), String.class);
    }
}
